/**
 * @author Tobias Boehm
 *
 * 03.06.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.ingame;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.match.Turn;
import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage;

/**
 * This server message should be used to announce a verified {@link Turn} to all
 * other players of a match.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchTurnAnnouncement extends AbsServerMatchMessage implements ICopyable<ServerMatchTurnAnnouncement> {

	private int mRoundID;
	private int mTurnID;
	private int mGameStateID;
	private boolean mTurnOpen;
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCH_TURN_ANNOUNCEMENT;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.server.messages.shared.AbsServerMatchMessage#onReadTransmissonMatchData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mTurnID = pDataInputStream.readInt();
		
		this.mTurnOpen = pDataInputStream.readBoolean();
		
		this.mGameStateID = pDataInputStream.readInt();
		
		this.mRoundID = pDataInputStream.readInt();
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.server.messages.shared.AbsServerMatchMessage#onWriteTransmissonMatchData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeInt(this.mTurnID);
		
		pDataOutputStream.writeBoolean(this.mTurnOpen);
		
		pDataOutputStream.writeInt(this.mGameStateID);
		
		pDataOutputStream.writeInt(this.mRoundID);
	}

	
	public void setTurn(final Turn pTurn){
		
		//this.mRoundID = pTurn.getRound().getRoundID();
		
		this.mTurnID = pTurn.getTurnID();
		
		this.mTurnOpen = pTurn.isTurnOpen();
		
		//this.mGameStateID = pTurn.getPlayerGameState().getGameStateID();
		
		// TODO: reparieren
	}

	@Override
	public ServerMatchTurnAnnouncement copy() {
		
		final ServerMatchTurnAnnouncement copy = new ServerMatchTurnAnnouncement();
		
		copy.mMatchID = this.mMatchID;
		copy.mRoundID = this.mRoundID;
		copy.mTurnID = this.mTurnID;
		copy.mTurnOpen = this.mTurnOpen;
		copy.mGameStateID = this.mGameStateID;
		
		return copy;
	}
}
